/*
 * @Author: Huangdong
 * @Date: 2022-03-28 15:21:05
 * @LastEditors: minsheng_insurance_agency_zhoucl
 * @LastEditTime: 2025-09-05 15:29:55
 * @FilePath: \01.latest-short-insure\vue.config.js
 * @Description:
 *
 *
 */
const fs = require("fs");
const autoprefixer = require("autoprefixer");
const pxtorem = require("postcss-pxtorem");
const timeStamp = require("dayjs")().valueOf();
let outputDirSet = "./dist/wxsc/";
process.env.VUE_APP_API = "https://wxsc.mschn.cn"; // xdd
process.env.VUE_APP_PATH = "/app_h5/latestshort/";
process.env.VUE_APP_VERSION = ""; // 控制json请求版本,可自定义的

// 配置静态页面访问
if (process.env.VUE_APP_PRODFLAG.indexOf("local") != -1) {
  process.env.VUE_APP_PATH = "./";
  outputDirSet = "./dist/local/";
  process.env.VUE_APP_API = "http://wxsc.mschn.cn";
}

if (process.env.VUE_APP_PRODFLAG == "wxsc") {
  process.env.VUE_APP_PATH = "/app_h5/testinsurance/latestshort/";
}

if (
  process.env.VUE_APP_PRODFLAG == "wxou" ||
  process.env.VUE_APP_PRODFLAG == "localpro"
) {
  process.env.VUE_APP_API = "/";
  outputDirSet = "./dist/wxou/";
}

/**版本管理方案 */
let packageJson = require("./src/config/version.json");
function addVersion(pJson) {
  let version = "1.0.0.1";
  if (!pJson) return version;
  // 将版本号切割成数组，计算出下一个版本号
  let arr = pJson.version.split(".").map((e) => {
    return parseInt(e);
  });
  arr[arr.length - 1] += 1;
  for (let i = arr.length - 1; i > 0; i--) {
    if (arr[i] > 9) {
      arr[i] = 0;
      if ([i - 1] > -1) arr[i - 1] += 1;
    }
  }
  return arr.join(".");
}
//设置开发环境下，每次启动不更新版本
if (process.env.NODE_ENV !== "development") {
  packageJson.version = addVersion(packageJson);
  process.env.VUE_APP_VERSION = packageJson.version || "";
  //往version.json中写入最新的版本号
  fs.writeFile(
    "./src/config/version.json",
    JSON.stringify(packageJson),
    (err) => {
      if (err) {
        console.error(err);
        return false;
      }
    }
  );
}

module.exports = {
  publicPath: process.env.VUE_APP_PATH,
  outputDir: outputDirSet,
  productionSourceMap: false,
  // 关闭eslint检查
  lintOnSave: false,
  // 配置css前缀,px转rem
  css: {
    loaderOptions: {
      postcss: {
        plugins: [
          autoprefixer(),
          pxtorem({
            rootValue: 37.5,
            propList: ["*"],
          }),
        ],
      },
    },
  },
  chainWebpack: (config) => {
    config.plugins.delete("prefetch");
    config.plugins.delete("preload");
    if (process.env.NODE_ENV === "production") {
      // 给js和css配置版本号
      config.output.filename(`js/v${packageJson.version}/[name].js`).end();
      config.output
        .chunkFilename(`js/v${packageJson.version}/[name].js?v=${timeStamp}`)
        .end();
      config.plugin("extract-css").tap((args) => [
        {
          filename: `css/v${packageJson.version}/[name].css`,
          chunkFilename: `css/v${packageJson.version}/[name].css?v=${timeStamp}`,
        },
      ]);
    }
  },
  devServer: {
    open: true, // 项目构建成功之后，自动弹出页面
    port: 8182, // 端口号，默认8080
    https: false, // 协议
    hotOnly: false, // 没啥效果，热模块，webpack已经做好了
  },
};
